<?php

namespace app\oa\model;

use think\Db;

class Mission extends Base
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    //定义相对的关联
    public function user()
    {
        return $this->belongsTo('user');
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description:获得任务列表
     * @time: 2017-7-12 15:53:58
     * @param $uid
     * @param int $start
     * @return array
     */
    static public function getMissionList($uid, $start = 0)
    {
        $list = Db::query("select * from oa_mission where FIND_IN_SET({$uid},part_res) ORDER BY id DESC limit {$start},10");
        $result_data = [];
        $partsPerson = [];
        foreach ($list as $index => $item) {
            $userData = Db::name('user')->field('realname')->find($item['user_id']);
            $parts = Db::name('user')->where(array('uid' => array('in', $item['part_res'])))->select();
            foreach ($parts as $item2) {
                $partsPerson[$index][] = $item2['realname'] . ':';
            }
            $item['create_person'] = $userData['realname'];
            $item['part_person_id'] = $item['part_res'];
            $item['partsPerson'] = implode($partsPerson[$index]);
            $result_data[$index] = $item;
        }
        return $result_data;
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: 获得任务数量
     * @time: 2017-7-12 15:55:56 修改时间  2017年7月13日 10:01:33
     * @param $uid
     * @return int
     */
    static public function getMissionListCount($uid)
    {
        $list = Db::query("select * from oa_mission where delete_time=0 AND FIND_IN_SET({$uid},part_res)");
        return (int)count($list);
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: 获得领取任务的Uid
     * @time: 2017年7月24日 10:58:44
     * @param $id
     * @param string $field
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getReceiveUidByMissionId($id, $field = '*')
    {
        $res = self::where(array('id' => $id))->field($field)->find();
        return $res;
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description:  修改领取的字段
     * @time: 2017年7月13日 10:59:16
     * @param： $id 任务id
     * @param: $str 要保存的字符串
     * @return bool
     */
    public static function modifyReceiveField($id, $str)
    {
        $res = Db::name('mission')->where(['id' => $id])->update(['is_get' => $str]);
        return $res == 1 ? true : false;
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: 修改任务状态
     * @time:2017年7月13日 16:22:25
     * @param $id
     * @return false|int
     */
    public static function modifyMissionCompleteStatus($id)
    {
        $res = Db::name('mission')->where(['id' => $id])->update(['is_complete' => 1]);
        return $res;
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: 搜索数据
     * @time:2017-7-26 10:34:15
     * @param $uid
     * @param $title
     * @return array
     */
    public static function search($uid,$title)
    {
        $list = Db::query("select * from oa_mission where FIND_IN_SET({$uid},part_res) AND `title` LIKE {$title} ORDER BY id DESC ");
        $result_data = [];
        $partsPerson = [];
        foreach ($list as $index => $item) {
            $userData = Db::name('user')->field('realname')->find($item['user_id']);
            $parts = Db::name('user')->where(array('uid' => array('in', $item['part_res'])))->select();
            foreach ($parts as $item2) {
                $partsPerson[$index][] = $item2['realname'] . ':';
            }
            $item['create_person'] = $userData['realname'];
            $item['part_person_id'] = $item['part_res'];
            $item['partsPerson'] = implode($partsPerson[$index]);
            $result_data[$index] = $item;
        }
        return $result_data;
    }
}
